package projetolp2.GraphicInterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import projetolp2.Sistem.Estabelecimento;
import projetolp2.Sistem.Sistema;

public class SugestoesLocal extends MealPoints implements ActionListener{

	private String nomePadrao = "Digite o local";
	private JPanel panelMenu = new JPanel(new BorderLayout());
	private JPanel panel1 = new JPanel(new GridLayout(0, 1));
	private JPanel panel2 = new JPanel(new GridLayout(0, 1));
	private JPanel panel3 = new JPanel(new GridLayout(0, 1));
	private JPanel panel4 = new JPanel(new GridLayout(0, 1));
	private JPanel panelFinal = new JPanel(new GridLayout(1,1));
	private JButton VOLTAR = new JButton(NomeBotoes.values()[idioma(IDIOMAPROGRAMA )].getVoltar());
	private JButton BUSCAR = new JButton("Buscar");
	private JLabel label;
	private int quantidade = 0;
	protected JTextField local = new JTextField(nomePadrao);
	
	public SugestoesLocal(){
		
	}
	private void ajudaDeBotoes(){
		VOLTAR.setToolTipText("Voltar a janela anterior");
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == VOLTAR){
			removerAdicionar(((SugestoesLocal) sugestoesLocal).panel(),sugestoespersonalizadas );
		}
		else if(e.getSource() == BUSCAR){
			removerAdicionar(((SugestoesLocal) sugestoesLocal).panel(), resultConsulta());
		}
	}
	public void quantidade(int valor){
		quantidade = valor;
	}
	private JPanel resultConsulta(){
		panel1.removeAll();
		panel2.removeAll();
		panel3.removeAll();
		panel4.removeAll();
		panelMenu.removeAll();
		panelFinal.removeAll();
		
		JList texto = new JList();
        String sugestoes = "Nome            -           Endereço;;;;********;";
        List<String> estabelecimentos = SISTEMA.converteEstabelecimentos(SISTEMA.recomendacaoPorLocalizacao(local.getText(), quantidade));
		int contador = 1;
		for(String i : estabelecimentos){
			sugestoes +=contador +" - "+i+";-------------------------------------;";
			contador++;
		}
	        try{
	        	final String valorees = sugestoes;
	        texto.setModel(new AbstractListModel() {
	        	String[] valores = valorees.split(";");
	            public int getSize() { return valores.length; }
	            public Object getElementAt(int i) { return valores[i]; }
	        });

	        texto.setName("jList1"); // NOI18N
	        }catch(Exception e){
	        	System.err.println(e.getMessage());
	        }
	        
		panel1.add(new JScrollPane(texto));
		panel2.add(VOLTAR);
        panel3.add(BUSCAR);
        panel4.add(local);

        panelFinal.add(panel4);
        panelFinal.add(panel2);
        panelFinal.add(panel3);
        panelMenu.add(panel1);
        panelMenu.add(panelFinal,BorderLayout.SOUTH);
        
		return panelMenu;
	}
	private void createComponents() {
		ajudaDeBotoes();
		//quantidadeSugestoes.pegaQuant();
		panelMenu.removeAll();
		panel1.removeAll();
		panel2.removeAll();
		ajudaDeBotoes();
		label = new JLabel("Os "+quantidade+" melhores restaurantes");

	    
		VOLTAR.setMnemonic(KeyEvent.VK_I);
		VOLTAR.addActionListener(this);
        label.setLabelFor(VOLTAR);
        
        BUSCAR.setMnemonic(KeyEvent.VK_I);
		BUSCAR.addActionListener(this);
        label.setLabelFor(BUSCAR);
		
        panel1.setBorder(BorderFactory.createEmptyBorder(
                10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panel2.add(VOLTAR);
        panel2.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panel3.add(BUSCAR);
        panel3.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panel4.add(local);
        panel4.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panelFinal.add(panel4);
        panelFinal.add(panel2);
        panelFinal.add(panel3);
        panelMenu.add(panel1);
        panelMenu.add(panelFinal,BorderLayout.SOUTH);
    }
	public Component panel(){
		createComponents();
		panelMenu.setBackground(Color.white);
		panelMenu.setVisible(true);
		
		
		return panelMenu;
	}	
}